package com.cip.master.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Locale;

import org.apache.struts2.ServletActionContext;

import com.cip.admin.model.Friends;
import com.cip.admin.model.User;
import com.cip.admin.service.FriendsService;
import com.cip.common.util.SecurityUtil;
import com.cip.master.model.Event;
import com.cip.master.service.EventService;
import com.cip.webapp.action.BaseAction;

public class EventAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private Event event;
	private List<Event> events;
	private List<User> unConformedFriends;
	private EventService eventService;
	private FriendsService friendsService;
	
	private File file;
	private String fileContentType;
	private String fileFileName;

	
	public String showEventForm() throws Exception{
		setEvent(new Event());
		return SUCCESS;
	}
	public String showEventList() throws Exception {
		
		Locale locale = getRequest().getLocale();
		String eventString = eventService.getEventGridList(locale,getRequest());
		getRequest().setAttribute("eventGridString", eventString);
		
//		setEvents(eventService.getAllEvent());
		return SUCCESS;
	}
	
	public String saveOrUpdateEvent() throws Exception{
		User user = SecurityUtil.getThreadLocalUser().get();
		event.setTenantId(user.getTenantId());
		
		if(null != file){
			// the directory to upload to
	        String uploadDir = ServletActionContext.getServletContext().getRealPath("/resources")
	                + "/" + getRequest().getRemoteUser() + "/";
	
	        // write the file to the file specified
	        File dirPath = new File(uploadDir);
	
	        if (!dirPath.exists()) {
	            dirPath.mkdirs();
	        }
	
	        //retrieve the file data
	        InputStream stream = new FileInputStream(file);
	
	        //write the file to the file specified
	        OutputStream bos = new FileOutputStream(uploadDir + fileFileName.replaceAll(" ", "_"));
	        int bytesRead;
	        byte[] buffer = new byte[8192];
	
	        while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
	            bos.write(buffer, 0, bytesRead);
	        }
	
	        bos.close();
	        stream.close();
			String uploadDirRelativePath="/resources" + "/" + getRequest().getRemoteUser() + "/";
	        event.setEventPicture(uploadDirRelativePath + fileFileName.replaceAll(" ", "_"));
		}
		eventService.saveOrUpdate(event);
		System.out.println("EVENT UPDATE SUCCESSFULLY-===============");
		saveMessage(getText("event.added"));
		return SUCCESS;
	}
	
	public String showEditEvent() {
		setEvent(eventService.getEvent(event.getId()));
		return SUCCESS;
	}
	
	/*public String showEventForm() {
		setEvent(new Event());
		return SUCCESS;
	}*/
	
	public String showUserEventList() throws Exception{	
		User user = SecurityUtil.getThreadLocalUser().get();
		List<User> users = friendsService.getAllUnConfirmedFriendsById(user.getId());
		setUnConformedFriends(friendsService.getAllUnConfirmedFriendsById(user.getId()));
		setEvents(eventService.getAllEvent());
		return SUCCESS;
	}
	
	public List<User> getUnConformedFriends() {
		return unConformedFriends;
	}
	public void setUnConformedFriends(List<User> unConformedFriends) {
		this.unConformedFriends = unConformedFriends;
	}
	public Event getEvent() {
		return event;
	}
	public void setEvent(Event event) {
		this.event = event;
	}
	public EventService getEventService() {
		return eventService;
	}
	public FriendsService getFriendsService() {
		return friendsService;
	}
	public void setFriendsService(FriendsService friendsService) {
		this.friendsService = friendsService;
	}
	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}
	public List<Event> getEvents() {
		return events;
	}
	public void setEvents(List<Event> events) {
		this.events = events;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
}
